﻿@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Associate your account with OAuth & OpenID providers";

    WebSecurity.RequireAuthenticatedUser();

    bool isSuccessful = false;
    bool associationFailed = false;
    string provider = null;
    
    if (IsPost) {
        provider = Request.Form["provider"];
        if (!provider.IsEmpty()) {
            OAuthWebSecurity.RequestAuthentication(provider);
            return;
        }
    } else {
        var result = OAuthWebSecurity.VerifyAuthentication();
        if (result.IsSuccessful) {
            string username = WebSecurity.CurrentUserName;
            OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, username);
            isSuccessful = true;
            provider = result.Provider;
        }
        else if (!result.Provider.IsEmpty()) {
            associationFailed = true;
            provider = result.Provider;
        }
    }
}

<hgroup class="title">
    <h1>@Page.Title.</h1>
    <h2>Choose a service to use to log in.</h2>
</hgroup>

@if (isSuccessful) {
    <p class="message-success">You have successfully associated this account with @provider.</p>
} else {
    if (associationFailed) {
        <p class="message-error">An error occurred while associating this account with @provider.</p>
    }
    <section class="social">
        <form method="post">
            <h2>Use another service to log in.</h2>
            <fieldset>
                <legend>Log in using another service</legend>
                <input type="submit" name="provider" id="facebook" value="Facebook" title="Log in using your Facebook account." />
                <input type="submit" name="provider" id="twitter" value="Twitter" title="Log in using your Twitter account." />
                <input type="submit" name="provider" id="windowsLive" value="Windows Live" title="Log in using your Windows Live account." />
            </fieldset>
        </form>
    </section>
}